package com.lyn.tencent_health_code.controller;

import com.lyn.tencent_health_code.annotation.Login;
import com.lyn.tencent_health_code.database.HealthCodeRecord;
import com.lyn.tencent_health_code.database.User;
import com.lyn.tencent_health_code.entity.LoginUser;
import com.lyn.tencent_health_code.entity.PersonInfo;
import com.lyn.tencent_health_code.entity.RequestResult;
import com.lyn.tencent_health_code.entity.VerifyHealthCode;
import com.lyn.tencent_health_code.service.EncrypDESService;
import com.lyn.tencent_health_code.service.HealthCodeRecordService;
import com.lyn.tencent_health_code.service.HealthCodeService;
import com.lyn.tencent_health_code.service.UserService;
import com.lyn.tencent_health_code.utils.TextUtils;
import com.lyn.tencent_health_code.utils.TokenUtils;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 健康码接口
 *
 * @author almost
 */
@Api("健康码接口")
@RestController
@RequestMapping(value = "/HealthCode")
public class HealthCodeController {
    private Logger logger= LoggerFactory.getLogger(HealthCodeController.class);
    @Autowired
    private HealthCodeService healthCodeService;
    @Autowired
    private EncrypDESService encrypDESService;
    @Autowired
    private UserService userService;
    @Autowired
    private HealthCodeRecordService healthCodeRecordService;

    @Login
    @GetMapping(value = "/getToken")
    public RequestResult getToken() {
        RequestResult result = new RequestResult();
        String healthCodeToken = null;
        try {
            healthCodeToken = healthCodeService.getHealthCodeToken();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (TextUtils.isEmpty(healthCodeToken)) {
            result.setCode(0);
            result.setMessage("获取token失败");
            return result;
        }
        result.setCode(1);
        result.setData(healthCodeToken);
        result.setLength(1);
        return result;
    }

    @Login
    @PostMapping(value = "/verifyHealthCode")
    public RequestResult verifyHealthCode(@RequestBody VerifyHealthCode verifyHealthCode, HttpServletRequest request) {
        RequestResult result = new RequestResult();
        PersonInfo personInfo=null;
        try {
            if (TextUtils.isEmpty(verifyHealthCode.getQrCode())){
                result.setMessage("健康码不能为空");
                return result;
            }
            if (TextUtils.isEmpty(verifyHealthCode.getSerialNumber())){
                result.setMessage("设备序列号不能为空");
                return result;
            }
            personInfo= healthCodeService.verifyHealthCode(verifyHealthCode);
            String token = TokenUtils.getToken(request);
            LoginUser loginUser = TokenUtils.getLoginUser(encrypDESService, token);
            if (loginUser==null){
                result.setMessage("获取失败");
                return result;
            }
            User user = userService.findByName(loginUser.getUserName());
            if (user==null){
                result.setMessage("获取失败");
                return result;
            }
            HealthCodeRecord healthCodeRecord=new HealthCodeRecord();
            healthCodeRecord.setUserId(user.getId());
            healthCodeRecord.setSerialNumber(verifyHealthCode.getSerialNumber());
            HealthCodeRecord save = healthCodeRecordService.save(healthCodeRecord);
            if (save==null){
                result.setMessage("获取失败");
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setData(e.getMessage());
            return result;
        }
        result.setCode(1);
        logger.info(personInfo.toString());
        result.setData(personInfo);
        result.setLength(1);
        return result;
    }
}
